$(document).ready(function(){
	setDialog();
	closeDialog();
	//初始化表格
	$('#dataGridTable').datagrid({
		columns:[[ 
           {field:'portalName',title:'面板名称',width:200,align:'center'}, 
           {field:'portalCode',title:'面板编码',width:200,align:'center'},
           {field:'portalState',title:'状态',width:80,align:'center',formatter:updateStates},
           {field:'portalIndex',title:'排序',width:80,align:'center'},
           {field:'createUser',title:'记录建立者',width:createUserWidth,align:'center'},
           {field:'createTime',title:'记录建立时间',width:createTimeWidth,align:'center'},
           {field:'lastModifiedUser',title:'记录最后修改者',width:lastModifiedWidth,align:'center'},
           {field:'lastModifiedTime',title:'记录最后修改时间',width:createTimeWidth,align:'center'}
         ]]
 	});
	$('#portalName').textbox('textbox').keydown(function (e) {
	   if (e.keyCode == 13) {
		   $('#portalName').textbox('setValue', $(this).val());
		   queryPortal();
	    }
	});
});

//保存面板
function savePortal(){
	var portalId=$("#portalIdInfo").val();
	var portalName=$("#portalNameInfo").val();
	var portalCode=$("#portalCodeInfo").val();
	var portalIndex=$("#portalIndexInfo").numberspinner('getValue');
	
	if(portalName==null||portalName==''){
		sysMsgShow("面板名称不允许为空!");
		return false;
	}else if(portalCode==null||portalCode==''){
		sysMsgShow("面板编码不允许为空!");
		return false;
	}else if(portalIndex==null||portalIndex==''){
		sysMsgShow("排序索引不允许为空!");
		return false;
	}else if(!isPInt(portalIndex)){
		sysMsgShow("排序索引必须是正整数!");
		return false;
	}else{

		$.ajax({
			async : false,
			cache:false,
			type: 'post',
			dataType : "json",
			data : {
				"portal.portalId" : portalId,
				"portal.portalName" : portalName,
				"portal.portalCode" : portalCode,
				"portal.portalIndex" : portalIndex
			},
			url: 'sys/savePortal.action',
			error: function () {
				sysErrorShow('请求失败!');
			},success:function(data){
				var messgage = portalId?"修改成功!":"添加成功!";
				if(data.success==true){
					reloadTable();
					closeDialog();
				}else{
					messgage = data.msg;
				}
				sysMsgShow(messgage);
			}
		});
	
	}	
}

//删除面板
function delPortal(){
	if($('#dataGridTable').datagrid('getSelected')){
		var portalCode = $('#dataGridTable').datagrid('getSelections')[0].portalCode;
		var ids = [];
		var cods = [];
		var selectedRow = $('#dataGridTable').datagrid('getSelections');
		for(var i=0;i<selectedRow.length;i++){
			ids.push(selectedRow[i].portalId);
			cods.push(selectedRow[i].portalName);
		}
		var portalId = ids.join(',');
		$.messager.confirm('删除提示', '你确定删除下列面板吗?<br/><br/>'+cods.join(','), function(r){
				if (r){
					var url = 'sys/deletePortal.action?portal.portalId='+portalId;
					$.ajax({
						async : false,
						cache:false,
						data : {"portal.portalCode":portalCode},
						type: 'post',
						dataType : "json",
						url:url,
						error: function () {
							sysErrorShow('请求失败');
						},success:function(data){
							if(data.success==true){
								sysMsgShow("删除成功!");
							}else{
								sysErrorShow(data.msg);
							}
							reloadTable();
						}
					});
				}
		});
	}
	else
	{
		noSelectData();
	}
}

//查询面板
function queryPortal(){
	$("#dataGridTable").datagrid('reload',{		
	 	'portal.portalName' : $("#portalName").val(),
	 	'portal.portalCode' : $("#portalCode").val()
	}); 
}

/**--------------操作弹出框------------------**/
//初始化模窗
function setDialog(){
	$('#portalInfoDialog').dialog({
		modal: true,
		collapsible : true,
		resizable : true,
		onClose : function(){
			resetDialog();
		}
	});
}

//打开添加或修改模窗
function openDialog_addPortal(){ 
	$('#portalInfoDialog').dialog({
		closed: false,
		modal:true,
        title: "新增面板",
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: savePortal
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#portalInfoDialog').dialog('close');                    
            }
        }]
    });
}

//打开编辑模窗
function openDialog_editPortal(){
	if($('#dataGridTable').datagrid('getSelected')){
		var portalId = $('#dataGridTable').datagrid('getSelections')[0].portalId;
		$.getJSON("sys/findPortalById.action?portal.portalId="+portalId,null,
		function msg(json){
			$("#portalIdInfo").val(portalId);
			$("#portalNameInfo").textbox('setValue',json.object.portalName);
			$("#portalCodeInfo").textbox('setValue',json.object.portalCode);
			$("#portalIndexInfo").numberspinner('setValue',json.object.portalIndex);
			openDialog_addPortal();
			$('#portalInfoDialog').dialog('setTitle','编辑面板'); 
		});
	}
	else
	{
		noSelectData();
	}
}

//关闭模窗
function closeDialog(){
	$('#portalInfoDialog').dialog('close');
}

//清空控件值
function resetDialog(){
	$("#portalIdInfo").val('');
	$("#portalNameInfo").textbox('setValue','');
	$("#portalCodeInfo").textbox('setValue','');
	$("#portalIndexInfo").numberspinner('setValue',"");
}

//刷新表格
function reloadTable(){
	$('#dataGridTable').datagrid('reload');
}

//刷新
function reloadPortal()
{
	$("#portalName").textbox('setValue','');
	queryPortal();
}
//改变状态
function enable(){
	var selectedData = $('#dataGridTable').datagrid('getSelections');
	if(selectedData.length > 0){
		var data = selectedData[0];
		var portalId = data.portalId;
		var portalState = data.portalState;
		$.ajax({
			async : false,
			cache:false,
			type: 'POST',
			dataType : "json",
			data : {
				"portal.portalId" : portalId,
				"portal.portalState" :portalState
			},
			url: 'sys/updatePortalStatus.action',//请求的action路径
			error: function () {//请求失败处理函数
				sysErrorShow('请求失败!');
			},success:function(data){
				if(data.success==true){//未返回任何消息表示添加成功
					sysMsgShow('操作成功!');
					queryPortal();
				}else{//返回异常信息
					if(data.msg='101')
					{
						sysMsgShow('修改状态失败!');
					}
				}
			}
		});
	} else {
		noSelectData();
	}
}

//状态图片
function updateStates(value,row){
	var portalState = row.portalState;
	if(portalState=='1'){
		portalState="<image src='../../resources/images/icons/yes.png'/>";
	}else if(portalState=='0'){
		portalState="<image src='../../resources/images/icons/stop.png'/>";
	}else{
		portalState='';
	}
	return portalState;
};
//禁用启用监听
function doEvent(rowIndex, rowData){
	var disableBtnFlag = false;
	var enableBtnFlag = false;
	//获得name值
	for(var i=0;i<resource.length;i++){
		var disableBtnName = $('#disableBtn').attr("name");
		if(resource[i].trim()==disableBtnName.trim())
		{
			disableBtnFlag = true;
		}
		if(resource[i].trim()==$('#enableBtn').attr("name"))
		{
			enableBtnFlag = true;
		}
	}
	if(1==rowData.portalState && !disableBtnFlag){
		$('#disableBtn').linkbutton("enable");
		$('#enableBtn').linkbutton("disable");
	}else{
		$('#disableBtn').linkbutton("disable");
	}
	if(0==rowData.portalState && !enableBtnFlag){
		$('#enableBtn').linkbutton("enable");
		$('#disableBtn').linkbutton("disable");
	}else{
		$('#enableBtn').linkbutton("disable");
	}
}
//判断正整数
function isPInt(str) {
	if( str == 0 )
	{
	    return false;
	}else{
		var re = /^([1-9]\d*|[0]{1,1})$/;
		return re.test(str);
	}
}